package com.chenwy.example.configuration;

import com.atomikos.jdbc.AtomikosDataSourceBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;

import java.sql.SQLException;
import java.util.Properties;

/**
 * Created by Administrator on 2019/5/7.
 */
public abstract class DBConfigBase {

    protected String url;
    protected String username;
    protected String password;
    protected String uniqueResourceName;
    protected String typeAliasesPackage;
    protected String packageToScan;

    @Autowired
    protected JpaVendorAdapter jpaVendorAdapter;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUniqueResourceName() {
        return uniqueResourceName;
    }

    public void setUniqueResourceName(String uniqueResourceName) {
        this.uniqueResourceName = uniqueResourceName;
    }

    public String getTypeAliasesPackage() {
        return typeAliasesPackage;
    }

    public void setTypeAliasesPackage(String typeAliasesPackage) {
        this.typeAliasesPackage = typeAliasesPackage;
    }

    public String getPackageToScan() {
        return packageToScan;
    }

    public void setPackageToScan(String packageToScan) {
        this.packageToScan = packageToScan;
    }



    protected AtomikosDataSourceBean  getAtomikosDataSource(DatabaseConfiguration databaseConfiguration) throws SQLException {
        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
        atomikosDataSourceBean.setPoolSize(databaseConfiguration.getPoolSize());
        atomikosDataSourceBean.setMinPoolSize(databaseConfiguration.getMinPoolSize());
        atomikosDataSourceBean.setMaxPoolSize(databaseConfiguration.getMaxPoolSize());
        atomikosDataSourceBean.setBorrowConnectionTimeout(databaseConfiguration.getBorrowConnectionTimeout());
        atomikosDataSourceBean.setReapTimeout(databaseConfiguration.getReapTimeout());
        atomikosDataSourceBean.setMaxIdleTime(databaseConfiguration.getMaxIdleTime());
        atomikosDataSourceBean.setMaintenanceInterval(databaseConfiguration.getMaintenanceInterval());
        atomikosDataSourceBean.setLoginTimeout(databaseConfiguration.getLoginTimeout());
        atomikosDataSourceBean.setTestQuery(databaseConfiguration.getTestQuery());
        atomikosDataSourceBean.setXaDataSourceClassName(databaseConfiguration.getXaDataSourceClassName());
        Properties properties = new Properties();
        properties.setProperty("user",username);
        properties.setProperty("url",url);
        properties.setProperty("password",password);
        atomikosDataSourceBean.setXaProperties(properties);
        atomikosDataSourceBean.setUniqueResourceName(uniqueResourceName);
        return atomikosDataSourceBean;
    }

    protected LocalContainerEntityManagerFactoryBean getEntityManagerFactory(AtomikosDataSourceBean dataSource){
        Properties properties = new Properties();
        properties.put("hibernate.transaction.jta.platform",AtomikosJtaPlatform.class.getName());
        properties.put("javax.persistence.transactionType", "JTA");
        LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter);
        entityManagerFactory.setJtaDataSource(dataSource);
        entityManagerFactory.setJpaProperties(properties);
        entityManagerFactory.setPackagesToScan(packageToScan);
        return entityManagerFactory;
    }
}
